package com.tjl.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjl.warehouse.exception.ThrowUtils;
import com.tjl.warehouse.mapper.*;
import com.tjl.warehouse.model.OutStore;
import com.tjl.warehouse.model.MyPage;
import com.tjl.warehouse.model.OutStore;
import com.tjl.warehouse.model.Product;
import com.tjl.warehouse.model.dto.OutStoreReq;
import com.tjl.warehouse.service.OutStoreService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Tang
* @description 针对表【out_store(出库单)】的数据库操作Service实现
* @createDate 2024-06-30 13:49:09
*/
@Service
public class OutStoreServiceImpl extends ServiceImpl<OutStoreMapper, OutStore>
    implements OutStoreService{

    @Resource
    private OutStoreMapper outStoreMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    ProductMapper productMapper;
    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public MyPage<OutStore> getPageList(OutStoreReq req, MyPage<OutStore> page) {
        ThrowUtils.throwIfParams(page == null);
        List<Integer> prodIds = null;
        //根据产品名 获取对应产品id列表
        if (StringUtils.isNotBlank(req.getProductName())){
            var productQuery = new LambdaQueryWrapper<Product>();
            productQuery.like(Product::getProductName, req.getProductName());
            prodIds = productMapper.selectList(productQuery)
                    .stream().map(Product::getProductId).toList();
        }

        String startTime = "1000-01-01 00:00:00";
        String endTime = "9999-12-30 23:59:59";
        if (StringUtils.isNotBlank(req.getStartTime())){
            startTime = req.getStartTime();
        }
        if (StringUtils.isNotBlank(req.getEndTime())){
            endTime = req.getEndTime();
        }

        var queryWrapper = new LambdaQueryWrapper<OutStore>();
        //仓库id
        queryWrapper.eq(req.getStoreId() != null, OutStore::getStoreId, req.getStoreId())
                //产品名
                .in(prodIds != null, OutStore::getProductId, prodIds)
                //是否出库
                .eq(req.getIsOut() != null,OutStore::getIsOut, req.getIsOut())
                //时间范围
                .between(!StringUtils.isAllBlank(req.getStartTime(),req.getEndTime()),
                        OutStore::getCreateTime, startTime, endTime);

        //查询
        IPage<OutStore> result = outStoreMapper.selectPage(page.toPage(), queryWrapper);
        //查询创建人
        result.getRecords().forEach(inStore -> {
            var user = userInfoMapper.selectById(inStore.getCreateBy());
            if (user != null)
                inStore.setUserCode(user.getUserCode());
        });
        //查询仓库名
        result.getRecords().forEach(inStore -> {
            var store = storeMapper.selectById(inStore.getStoreId());
            if (store != null)
                inStore.setStoreName(store.getStoreName());
        });
        //查询产品名
        result.getRecords().forEach(inStore -> {
            var product = productMapper.selectById(inStore.getProductId());
            if (product != null)
                inStore.setProductName(product.getProductName());
        });
        return new MyPage<>(result);
    }
}




